﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class TheoDoiXeBeTong : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmTheoDoiXeBeTong";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 7;
    static int CurrentPage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {
                string idnguoidung = Session["IDUser"].ToString();
                p = new SqlParameter[2];
                p[0] = new SqlParameter("@idnguoidung", idnguoidung);
                p[0].SqlDbType = SqlDbType.NVarChar;
                p[1] = new SqlParameter("@tag", frmName);
                p[1].SqlDbType = SqlDbType.NVarChar;
                DataTable dt2 = ex.GetData("sp_xdacacchucnang_list", p);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;
                    Title = "Thiết lập theo dõi xe bê tông | " + Header;
                    btnCancel_Click(sender, e);
                    btnNext.Visible = false;
                    btnPre.Visible = false;
                    btnFirst.Visible = false;
                    LoadBienSo(dlBienSo);
                    LoadKhachHang(dlKhachHang, "--Chọn khách hàng--");
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_xdatheodoixebetong_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {
                    txtTuNgay.Text = dt.Rows[0]["Ngay"].ToString();
                    dlBienSo.SelectedValue = dt.Rows[0]["BienSo"].ToString();
                    dlKhachHang.SelectedValue = dt.Rows[0]["KhachHang"].ToString();
                    txtDiemXuatPhat.Text = dt.Rows[0]["DiemXuatPhat"].ToString();
                    txtNoiDen.Text = dt.Rows[0]["NoiDen"].ToString();
                    txtGPS.Text = dt.Rows[0]["GPS"].ToString();
                    txtDongHo.Text = dt.Rows[0]["DongHo"].ToString();
                    txtThucTe.Text = dt.Rows[0]["ThucTe"].ToString();

                    txtLoaiDuong.Text = dt.Rows[0]["LoaiDuong"].ToString();
                    txtNhienLieu.Text = dt.Rows[0]["NhienLieu"].ToString();
                    txtChotKM.Text = dt.Rows[0]["ChotKM"].ToString();
                    txtChieuCao1.Text = dt.Rows[0]["ChieuCaoBD1"].ToString();
                    txtChieuCao2.Text = dt.Rows[0]["ChieuCaoBD2"].ToString();
                    txtSoNLTH.Text = dt.Rows[0]["NhienLieuTieuHao"].ToString();
                    txtConLai.Text = dt.Rows[0]["ConLai"].ToString();
                    txtDinhMuc.Text = dt.Rows[0]["DinhMucNgay"].ToString();
                    txtDMLanNap.Text=dt.Rows[0]["DinhMucNap"].ToString();

                }
                catch (Exception ax)
                {
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tTheo dõi xe bê tông\t" + ax.Message);
                    gstGetMess(ax.Message, "");
                }

            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    try
                    {
                        ex.ExecuteNonQuerry("sp_xdatheodoixebetong_Del", p);
                        btnCancel_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tTheo dõi xe bê tông\t" + ax.Message);
                        gstGetMess(ax.Message, "");
                    }
                }
                else
                    gstGetMess("Bạn không có quyền xóa dữ liệu trên trang này", "");
            }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                p = new SqlParameter[18];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@BienSo", ex.GetGuid(dlBienSo.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@KhachHang", ex.GetGuid(dlKhachHang.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtTuNgay.Text.Trim()));
                p[3].SqlDbType = SqlDbType.SmallDateTime;
                p[4] = new SqlParameter("@DiemXuatPhat", txtDiemXuatPhat.Text.Trim());
                p[4].SqlDbType = SqlDbType.NVarChar;
                p[5] = new SqlParameter("@NoiDen", txtNoiDen.Text.Trim());
                p[5].SqlDbType = SqlDbType.NVarChar;
                p[6] = new SqlParameter("@GPS", double.Parse(txtGPS.Text.Trim().Equals("") ? "0" : txtGPS.Text.Trim()));
                p[6].SqlDbType = SqlDbType.Float;
                p[7] = new SqlParameter("@DongHo", double.Parse(txtDongHo.Text.Trim().Equals("") ? "0" : txtDongHo.Text.Trim()));
                p[7].SqlDbType = SqlDbType.Float;
                p[8] = new SqlParameter("@ThucTe", double.Parse(txtThucTe.Text.Trim().Equals("") ? "0" : txtThucTe.Text.Trim()));
                p[8].SqlDbType = SqlDbType.Float;
                p[9] = new SqlParameter("@LoaiDuong", txtLoaiDuong.Text.Trim());
                p[9].SqlDbType = SqlDbType.NVarChar;
                p[10] = new SqlParameter("@NhienLieu", double.Parse(txtNhienLieu.Text.Trim().Equals("") ? "0" : txtNhienLieu.Text.Trim()));
                p[10].SqlDbType = SqlDbType.Float;
                p[11] = new SqlParameter("@ChotKM", double.Parse(txtChotKM.Text.Trim().Equals("") ? "0" : txtChotKM.Text.Trim()));
                p[11].SqlDbType = SqlDbType.Float;
                p[12] = new SqlParameter("@ChieuCaoBD1", double.Parse(txtChieuCao1.Text.Trim().Equals("") ? "0" : txtChieuCao1.Text.Trim()));
                p[12].SqlDbType = SqlDbType.Float;
                p[13] = new SqlParameter("@ChieuCaoBD2", double.Parse(txtChieuCao2.Text.Trim().Equals("") ? "0" : txtChieuCao2.Text.Trim()));
                p[13].SqlDbType = SqlDbType.Float;
                p[14] = new SqlParameter("@NhienLieuTieuHao", double.Parse(txtSoNLTH.Text.Trim().Equals("") ? "0" : txtSoNLTH.Text.Trim()));
                p[14].SqlDbType = SqlDbType.Float;
                p[15] = new SqlParameter("@ConLai", double.Parse(txtConLai.Text.Trim().Equals("") ? "0" : txtConLai.Text.Trim()));
                p[15].SqlDbType = SqlDbType.Float;
                p[16] = new SqlParameter("@DinhMucNgay", double.Parse(txtDinhMuc.Text.Trim().Equals("") ? "0" : txtDinhMuc.Text.Trim()));
                p[16].SqlDbType = SqlDbType.Float;
                p[17] = new SqlParameter("@DinhMucNap", double.Parse(txtDMLanNap.Text.Trim().Equals("") ? "0" : txtDMLanNap.Text.Trim()));
                p[17].SqlDbType = SqlDbType.Float;

                ex.ExecuteNonQuerry("sp_xdatheodoixebetong_Add", p);
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {

                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tTheo dõi xe bê tông\t" + ax.Message);
                gstGetMess(ax.Message, "");
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (f_Sua.Equals(1))
        {
            try
            {
                Guid gID = new Guid(hdID.Value);
                p = new SqlParameter[18];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@BienSo", ex.GetGuid(dlBienSo.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@KhachHang", ex.GetGuid(dlKhachHang.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtTuNgay.Text.Trim()));
                p[3].SqlDbType = SqlDbType.SmallDateTime;
                p[4] = new SqlParameter("@DiemXuatPhat", txtDiemXuatPhat.Text.Trim());
                p[4].SqlDbType = SqlDbType.NVarChar;
                p[5] = new SqlParameter("@NoiDen", txtNoiDen.Text.Trim());
                p[5].SqlDbType = SqlDbType.NVarChar;
                p[6] = new SqlParameter("@GPS", double.Parse(txtGPS.Text.Trim().Equals("") ? "0" : txtGPS.Text.Trim()));
                p[6].SqlDbType = SqlDbType.Float;
                p[7] = new SqlParameter("@DongHo", double.Parse(txtDongHo.Text.Trim().Equals("") ? "0" : txtDongHo.Text.Trim()));
                p[7].SqlDbType = SqlDbType.Float;
                p[8] = new SqlParameter("@ThucTe", double.Parse(txtThucTe.Text.Trim().Equals("") ? "0" : txtThucTe.Text.Trim()));
                p[8].SqlDbType = SqlDbType.Float;
                p[9] = new SqlParameter("@LoaiDuong", txtLoaiDuong.Text.Trim());
                p[9].SqlDbType = SqlDbType.NVarChar;
                p[10] = new SqlParameter("@NhienLieu", double.Parse(txtNhienLieu.Text.Trim().Equals("") ? "0" : txtNhienLieu.Text.Trim()));
                p[10].SqlDbType = SqlDbType.Float;
                p[11] = new SqlParameter("@ChotKM", double.Parse(txtChotKM.Text.Trim().Equals("") ? "0" : txtChotKM.Text.Trim()));
                p[11].SqlDbType = SqlDbType.Float;
                p[12] = new SqlParameter("@ChieuCaoBD1", double.Parse(txtChieuCao1.Text.Trim().Equals("") ? "0" : txtChieuCao1.Text.Trim()));
                p[12].SqlDbType = SqlDbType.Float;
                p[13] = new SqlParameter("@ChieuCaoBD2", double.Parse(txtChieuCao2.Text.Trim().Equals("") ? "0" : txtChieuCao2.Text.Trim()));
                p[13].SqlDbType = SqlDbType.Float;
                p[14] = new SqlParameter("@NhienLieuTieuHao", double.Parse(txtSoNLTH.Text.Trim().Equals("") ? "0" : txtSoNLTH.Text.Trim()));
                p[14].SqlDbType = SqlDbType.Float;
                p[15] = new SqlParameter("@ConLai", double.Parse(txtConLai.Text.Trim().Equals("") ? "0" : txtConLai.Text.Trim()));
                p[15].SqlDbType = SqlDbType.Float;
                p[16] = new SqlParameter("@DinhMucNgay", double.Parse(txtDinhMuc.Text.Trim().Equals("") ? "0" : txtDinhMuc.Text.Trim()));
                p[16].SqlDbType = SqlDbType.Float;
                p[17] = new SqlParameter("@DinhMucNap", double.Parse(txtDMLanNap.Text.Trim().Equals("") ? "0" : txtDMLanNap.Text.Trim()));
                p[17].SqlDbType = SqlDbType.Float;

                ex.ExecuteNonQuerry("sp_xdatheodoixebetong_Update", p);
                btnCancel_Click(sender, e);
                gstGetMess("Cập nhật thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess(ax.Message, "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tTheo dõi xe bê tông\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền sửa dữ liệu trên trang này", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        txtTuNgay.Text = "";
        dlBienSo.SelectedIndex = 0;
        dlKhachHang.SelectedIndex = 0;
        txtDiemXuatPhat.Text = "";
        txtNoiDen.Text = "";
        txtGPS.Text = "";
        txtDongHo.Text = "";
        txtThucTe.Text = "";

        txtLoaiDuong.Text = "";
        txtNhienLieu.Text = "";
        txtChotKM.Text = "";
        txtChieuCao1.Text = "";
        txtChieuCao2.Text = "";
        txtSoNLTH.Text = "";
        txtConLai.Text = "";
        txtDinhMuc.Text = "";
        txtDMLanNap.Text = "";

        GV.DataSource = ex.GetData("sp_xdatheodoixebetong_List");
        GV.DataBind();
        

        btnSave.Visible = true;
        btncapnhat.Visible = false;


    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdatheodoixebetong_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdatheodoixebetong_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdatheodoixebetong_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }
    void LoadKhachHang(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdakhachhang_ListDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
    void LoadBienSo(DropDownList dlBienSo)
    {
        dlBienSo.Items.Clear();
        dlBienSo.DataSource = ex.GetData("sp_xdatheodoixe_LoadBienSo");
        dlBienSo.DataBind();
        dlBienSo.Items.Insert(0, new ListItem("--Chọn biển số--", ""));
        dlBienSo.SelectedIndex = 0;
    }
    protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGrid = (GridView)sender;
            GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            HeaderGridRow.Height = 20;
            HeaderGridRow.HorizontalAlign = HorizontalAlign.Center;
            TableCell HeaderCell = new TableCell();

            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 6;


            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "Số km vận chuyển";
            HeaderCell.ColumnSpan = 3;

            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 4;

            
            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "Chiều cao bình dầu";
            HeaderCell.ColumnSpan = 2;


            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "Số nhiên liệu";
            HeaderCell.ColumnSpan = 2;

            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "Định mức";
            HeaderCell.ColumnSpan = 2;

            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 1;

            GV.Controls[0].Controls.AddAt(0, HeaderGridRow);
        }
    }
}